package com.yf.bean;

import com.yf.utils.Utils;
import lombok.Data;
import org.apache.commons.codec.digest.MurmurHash3;

import java.sql.Timestamp;

@Data
public class UserBean {
    Integer id;
    String name;
    Integer nameID;
    String email;
    Integer emailID;
    String passwd;
    Boolean valid;
    Timestamp created;
    Timestamp lastLoginTime;
    Integer role;

    public boolean valid() {
        if (Utils.isNullOrEmpty(name) || name.length() > 20) {
            return false;
        }

        if (Utils.isNullOrEmpty(email) || email.length() > 30) {
            return false;
        }

        if (Utils.isNullOrEmpty(passwd) || passwd.length() > 30) {
            return false;
        }

        return true;
    }

    public void transform() {
        if (!Utils.isNullOrEmpty(passwd)) {
            passwd = Utils.digestPasswd(passwd);
        }

        if (!Utils.isNullOrEmpty(email)) {
            emailID =  MurmurHash3.hash32x86(email.getBytes());
        }

        if (!Utils.isNullOrEmpty(name)) {
            nameID = MurmurHash3.hash32x86(name.getBytes());
        }
    }
}
